Keyword parameters are closely tied with optional parameters.
So here, we have our function f that takes in three arguments x, y,
and z, and y and z have default values of tree and seven.
Now, here on line five we can see that we can call our function by only providing x,
and then y and z get their default values.
On line six here,
we call our function with x equals two,
and y equals five,
and z getting its default value.
But suppose we wanted to call our function and provide a value for
x and z without actually passing in a value for y.
So, in other words, we want to give y whatever its default value should be.
So, if we wanted to do this,
we would want to kind of skip a value for y.
If we tried to do something like skipping y by saying,
two comma blank comma eight,
then we would get a syntax error because Python doesn't understand this format.
Instead, what we can do is we can use a keyword parameter.
So, rather than skipping an argument,
we can just say that this eight is intended to be the value for z.
We do that by saying z equals eight when we call our function.
Now, what this does is x gets the value two and z gets the value eight.
We never specified a value for y,
so y gets its value three.
When we run our code,
we can see that in the second call here that this is the case.
So again, x is two,
z is eight and then y is it's default value.
So, these are keyword arguments.
We could also do this for x.
So, we could say x equals two and z equals eight,
and this would have the same outcome.
The nice thing about keyword arguments,
is that they allow us to put our arguments in any order.
So here, I'll say x equals 20 and z equals eight,
but I'll specify z before I specify x.
Now, note that keyword arguments aren't
going to give arguments that don't have default values,
default values. So, for example.
If I tried to call our function f and just specified z equals eight,
then we haven't passed in anything for x and we get
an error because we haven't actually specified what x is.
So, if we wanted to do this we would have to specify a value for x somewhere or we
would need to give x a default value in our function definition.
So, it's also possible to
accidentally specify multiple values for the same argument here.
So, the easy case to see it would
be if we specified z equals eight and then z equals four.
If we did that, we would see that we have a keyword argument repeated online five.
It's less easy to see if we accidentally do it by saying something like,
passing in 10 for x by passing it in as the first argument,
and then later on accidentally specifying that x equals eight.
Here, we get the same kind of error.
So, we see that we have multiple values for the argument x,
online five and that's because here it looks
like x should be 10 by virtue of the fact that we pass 10 in as the first argument.
Then here, it looks like x should be eight
by virtue of the fact that we specified x equals eight.
Another thing to note about keyword parameters are that
keyword parameters always have to be expressed after positional arguments.
So, I'm going to modify our code to say that x should be 10 and z should be eight,
and this works just fine.
But if I change the order of this to say that z equals eight and then we pass in 10,
then we're going to get a syntax error.
That's because again, keyword arguments always have to come
after any positional arguments that don't specify a keyword.
So, in other words,
we have to put z equals eight after specifying that x is 10.
So, if I change the order here again,
then we'll see that our code is fixed.
Now, let's look at some questions that involve keyword arguments.
So, in this question we're asked.
What value will be printed for z?
So, we call our function f,
we specify x is two,
we specify y is five,
and then we have z equals initial.
So, what that means is z is going to be
the value of initial as soon as this function is declared.
We can see that initial is set here,
and so the value of z is going to be seven.
In this question we're asked.
What value will be printed for y?
So, we have the same function definition.
So, we specify initial equals seven and then
we provide default values for y which is going to be three,
and for z which is going to be seven.
Then, when we call our function f,
we pass in a value for x,
which is two and a value for z, which is 10.
So, what that means, is that y gets its default value, which is three.
So, we should get three printed out for y.
In this question we're asked.
What value will be printed for x?
So, we have the same function definition x, y, and z.
When we call our function f,
we specify that X is two here.
But, then we also specified that x is five,
and this isn't going to fly.
Python is going to give us a runtime error because we tried to
specify two different values for x, two and five.
So, the answer here is E. In this question we're asked.
What value will be printed for z?
So, we have the same function definition as before arguments x, y, and z.
We specify that z's default value is going to be initial.
When we define our function,
the value of initial is seven,
but it just so happens that we overwrite that value later on so that it's zero.
But again, these default values are only evaluated when we declare the function.
When we declare this function f initial had the value seven,
and so we can just almost replaced this with the value seven.
It doesn't matter that we changed initial later on,
z is going to have the value seven by default.
So, the answer here is B.
That's all for now, until next time.